﻿using System;
using System.Collections;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{
	//负责在Manifest中查找CAB文件。
	class CABFileFinder
	{
		
		//按名查找，未找到抛出异常。
		public static CABFile findCABFile(Manifest manifest, string name)
		{
			if( name.EndsWith( "." ) )
			{
				name = name.Substring( 0, name.Length - 1 );
			}
			CABFile cabFile = null;
			ArrayList cabFileList = manifest.CabFileList;
			foreach (CABFile curCABFile in cabFileList)
			{
				if (curCABFile.Name.ToLower().Equals(name.ToLower()))
				{
					cabFile = curCABFile;
					break;
				}
			}
			if (cabFile == null)
			{
				throw new InfoJetException("Cannot find the cab file " + name, "MU01");
			}
			return cabFile;
		}

		//按名查找，未找到返回null，不抛出异常。
		public static CABFile lookupCABFile(Manifest manifest, string name)
		{
			//CID 0422 //2009.10.14 //Begin
			if( name.EndsWith( "." ) )
			{
				name = name.Substring( 0, name.Length - 1 );
			}
			//CID 0422 //2009.10.14 //End
            //CID 0683 //2010.10.19 //Begin
            if (name.StartsWith("x-soln:///"))
            {
                name = name.Substring(10, name.Length-10);
            }
            //CID 0683 //2010.10.19 //End
			CABFile cabFile = null;
			ArrayList cabFileList = manifest.CabFileList;
			foreach (CABFile curCABFile in cabFileList)
			{
				if (curCABFile.Name.ToLower().Equals(name.ToLower()))
				{
					cabFile = curCABFile;
					break;
				}
			}
			return cabFile;
		}

	}
}